import dayjs from 'dayjs'
import { WX_OCR_SERVICEID } from '@/config/index.js'

/**
 * 格式化开始时间
 */
export const formatBeginTime = (time) => {
	const now = dayjs()
	const begin = dayjs(time)
	const diffDay = now.diff(begin, 'day')

	if (diffDay > 0) {
		if (diffDay > 7) {
			return time
		}
		return `${diffDay}天前`
	} else {
		const diffHour = now.diff(begin, 'hour')
		if (diffHour > 0) {
			return `${diffHour}小时前`
		} else {
			const diffMin = now.diff(begin, 'minute')
			return diffMin > 0 ? `${diffMin}分钟前` : '刚刚'
		}
	}
}

/**
 * 格式化结束剩余时间
 */
export const formatEndTime = (time) => {
	const now = dayjs()
	const end = dayjs(time)
	const diffDay = end.diff(now, 'day')
	let diffHour = end.diff(now, 'hour')
	let diffMin = end.diff(now, 'minute')
	let rest = ''
	if (diffDay > 0) {
		rest += `${diffDay}天`
		diffHour = diffDay % 24
	}
	if (diffHour > 0) {
		rest += `${diffHour}小时`
		diffMin = diffMin % 60
	}
	if (diffMin > 0) {
		rest += `${diffMin}分钟`
	}
	return rest
}
export function mathHeight(h,type){
	let systemInfo = uni.getSystemInfoSync()
	// let pxToRpxScale = systemInfo.windowWidth<=750 ? 750 / systemInfo.windowWidth:systemInfo.devicePixelRatio;
	// console.log(systemInfo)
	// let pxToRpxScale = systemInfo.windowWidth<=750 ? 2:systemInfo.devicePixelRatio;
	var res =  ((systemInfo.windowHeight- h - systemInfo.statusBarHeight))
	if (type!=false){
		res = res + 'px'
	}
	return res
}
//处理千分位使用
export function dealThousands(value) {
    if (value === 0) {
        return parseFloat(value);
    }
	var num = "";
    if (value != "") {
		if(!value){
			return num;
		}
        value += "";//转化成字符串
        value = parseFloat(value.replace(/,/g, ''))+'';//若需要其他小数精度，可将2改成变量
        if (value.indexOf(".") == -1) {
            num = value.replace(/\d{1,3}(?=(\d{3})+$)/g, function(s) {
                return s + ',';
            });
        } else {
            num = value.replace(/(\d)(?=(\d{3})+\.)/g, function(s) {
                return s + ',';
            });
        }
    } else {
        num = ""
    }
    return num;
}
const subOrderState = {
		1:'待核销',
		2:'已核销',
		3:'已取消',
		4:'已过期',
		5:'已结算'
	}
export function getOrderStatus(status) {
	return subOrderState[status]||''
}
// 判断邮件是否正确
export function validateEmail(email) {
    // 定义电子邮件地址的正则表达式模式
    var pattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
    
    if (pattern.test(email)) {
        return true; // 如果符合规则返回true
    } else {
        return false; // 不符合规则返回false
    }
}
export function checkPhoneNumber(phone) {
    var regExp = /^1[3456789]\d{9}$/; // 中国大陆手机号的正则表达式
    
    if (regExp.test(phone)) {
        return true; // 符合手机号格式要求
    } else {
        return false; // 不符合手机号格式要求
    }
}

/**
 * 小程序码参数解析
 * @param {String} scene
 * @returns {Object}
 */
export function parseScene(scene) {
	const obj = {}
	if (!scene) {
		return obj
	} else {
		scene = decodeURIComponent(scene)
		const arr = scene.split('&') || []
		if (arr.length) {
			arr.forEach(val => {
				const keyValue = val.split('=')
				if (keyValue.length > 1) {
					obj[keyValue[0]] = keyValue[1]
				}
			})
			return obj
		} else {
			const keyValue = scene.split('=')
			if (keyValue.length > 1) {
				obj[keyValue[0]] = keyValue[1]
			}
			return obj
		}
	}
}
export function judegDit(num){
	var reg= /^(-?\d+)(\.\d{1,2})?$/
	if(reg.test(num+''))
	{
		return true
	}else{
		return false
	}
}

export function formatDatetime (datetime,formatStr){
	const newDateTime = reformatDatetime(datetime);
	const result = dayjs(newDateTime).format(formatStr)
	return result==='Invalid Date'?'':result
}

export const ocrImage = async (imgUrl,ocrType)=>{
	return await wx.serviceMarket.invokeService({
		service: WX_OCR_SERVICEID,
		api: 'OcrAllInOne',
		data: {
			img_url: imgUrl,
			data_type: 3, // 1：二进制；2：base64字符串；3：图片url
			ocr_type: ocrType // 1：身份证；2：银行卡；3：行驶证；4：驾驶证；7：营业执照；8：通用OCR ; 10: 车牌识别
		},
	});
}